# coding:utf8
import urllib.request
from bs4 import BeautifulSoup
from Jdbc import Jdbc

class ZGWKDownload():

    def domnload(self,url):
        # 设置头信息
        header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5702.400 QQBrowser/10.2.1893.400'
        }
        request = urllib.request.Request(url, headers=header)
        # 下载网页1
        response1 = urllib.request.urlopen(request)
        # print(response1.read())
        # 通过html.parser分析页面
        suop = BeautifulSoup(response1, 'html.parser', from_encoding='utf-8')
        response1.close()
        strs = suop.find(class_='bf-box_text contact moduleBoxbg')
        comIntroL = suop.find(class_='cname fl')

        # print(strs)
        if strs != None:
            name = strs.find(class_='name')
            phoneNumber = strs.find(class_='phoneNumber')
            telephoneShow = strs.find(class_='addR telephoneShow')
            addR = strs.find(id='detialAddr')

            # print(name.getText().strip())
            # print(phoneNumber.getText().strip())
            # print(comIntroL.getText().strip())
            # print(telephoneShow.getText().strip())
            # print(addR.getText().strip())

            company = list()
            if comIntroL != None:
                company.append(comIntroL.getText().strip())
            else:
                company.append('')
            if addR != None:
                company.append(addR.getText().strip())
            else:
                company.append('')
            if name != None:
                company.append(name.getText().strip())
            else:
                company.append('')
            if telephoneShow != None:
                company.append(telephoneShow.getText().strip())
            else:
                company.append('')
            if phoneNumber != None:
                company.append(phoneNumber.getText().strip())
            else:
                company.append('')
            company.append('')
            company.append('')
            company.append('')
            company.append('')
            company.append('')
            company.append('')
            company.append('中国网库')
            company.append('0')
            company.append('')

            jdbc = Jdbc()
            jdbc.addCompany(company)
        else:
            self.domnload(url+"/ch6")
